<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>角色新增页面</title>
    <link rel="stylesheet" th:href="@{/pear/component/pear/css/pear.css}"/>
</head>
<body>
<form class="layui-form" method="POST" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="layui-form-item">
                <label class="layui-form-label">角色名称</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="roleName" maxlength="10" lay-verify="required｜roleName"
                               autocomplete="off"
                               placeholder="请输入角色名称"
                               class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色代码</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="roleCode" maxlength="20" lay-verify="required|roleCode"
                               autocomplete="off"
                               placeholder="请输入角色代码"
                               class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色类型</label>
                <div class="layui-input-block">
                    <input type="radio" name="type" value="0" title="部门角色">
                    <input type="radio" name="type" value="1" title="个人角色">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">角色描述</label>
                <div class="layui-input-block">
                    <label>
                        <input type="text" name="description" maxlength="30" lay-verify="description" autocomplete="off"
                               placeholder="请输入角色描述"
                               class="layui-input">
                    </label>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">分配权限</label>
                <div style="display: flex; align-items: flex-start;">
                    <div class="layui-input-block" style="margin-left: 0;">
                        <h6>功能权限</h6>
                        <div id="ID-tree-demo-showCheckbox"></div>
                    </div>
                    <div class="layui-input-block" style="margin-left: 10px;"> <!-- 设置较小的margin-left值 -->
                        <h6>数据权限(栏目)</h6>
                        <div id="ID-tree-demo-showCheckbox1"></div>
                    </div>
                    <div class="layui-input-block" style="margin-left: 10px;"> <!-- 设置较小的margin-left值 -->
                        <h6>数据权限(信息)</h6>
                        <div id="ID-tree-demo-showCheckbox2"></div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn  layui-btn-sm" lay-submit=""
                    lay-filter="role-save" id="role-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script th:src="@{/pear/component/layui/layui.js}"></script>
<script th:src="@{/pear/component/pear/pear.js}"></script>
<script th:src="@{/pear/component/layui/authtree.js}"></script>
<script th:inline="javascript">
    layui.use(['form', 'jquery', 'authtree'], function () {
        let baseURL = /*[[@{/}]]*/ '';
        let form = layui.form;
        let $ = layui.jquery;
        const authtree = layui.authtree;
        let checked = [];
        let channelIds = [];
        let infoIds = [];
        $.ajax({
            url: baseURL + "permission/list",
            type: 'get',
            success: function (res) {
                const trees = authtree.listConvert(res.data, {
                    primaryKey: 'permissionId'
                    , startPid: 0
                    , parentKey: 'permissionParentId'
                    , nameKey: 'permissionName'
                    , valueKey: 'permissionId'
                    , checkedKey: res.data.permissionId
                });
                authtree.render('#ID-tree-demo-showCheckbox', trees, {
                    autoWidth: true,
                    openChecked: true,
                    autochecked: false,
                    autoclose: false
                });
                authtree.showDept('#ID-tree-demo-showCheckbox', 1);
                authtree.on('change(#ID-tree-demo-showCheckbox)', function () {
                    // 获取所有已选中节点
                    checked = authtree.getChecked('#ID-tree-demo-showCheckbox');
                });
            }
        })
        // 渲染父级栏目树形结构
        $.ajax({
            url: baseURL + "cms/channel/getChannelList",
            type: 'get',
            success: function (res) {
                const trees = authtree.listConvert(res.data, {
                    primaryKey: 'channelId'
                    , startPid: 0
                    , parentKey: 'parentId'
                    , nameKey: 'channelName'
                    , valueKey: 'channelId'
                    , checkedKey: res.data.channelId
                });
                authtree.render('#ID-tree-demo-showCheckbox1', trees, {
                    autoWidth: true,
                    openChecked: true,
                    autochecked: false,
                    autoclose: false
                });
                authtree.showDept('#ID-tree-demo-showCheckbox1', 2);
                authtree.on('change(#ID-tree-demo-showCheckbox1)', function () {
                    // 获取所有已选中节点
                    channelIds = authtree.getChecked('#ID-tree-demo-showCheckbox1');
                });
            }
        })
        // 渲染信息树形结构
        $.ajax({
            url: baseURL + "cms/channel/getChannelList",
            type: 'get',
            success: function (res) {
                const trees = authtree.listConvert(res.data, {
                    primaryKey: 'channelId'
                    , startPid: 0
                    , parentKey: 'parentId'
                    , nameKey: 'channelName'
                    , valueKey: 'channelId'
                    , checkedKey: res.data.channelId
                });
                authtree.render('#ID-tree-demo-showCheckbox2', trees, {
                    autoWidth: true,
                    openChecked: true,
                    autochecked: false,
                    autoclose: false
                });
                authtree.showDept('#ID-tree-demo-showCheckbox2', 2);
                authtree.on('change(#ID-tree-demo-showCheckbox2)', function () {
                    // 获取所有已选中节点
                    infoIds = authtree.getChecked('#ID-tree-demo-showCheckbox2');
                });
            }
        })
        // 渲染
        form.on('submit(role-save)', function (data) {
            // 获取提交按钮
            let submitButton = $('#role-save');
            // 禁用按钮，防止重复提交
            submitButton.addClass('layui-btn-disabled').attr('disabled', 'disabled');
            let formData = {
                roleName: data.field.roleName, // 角色名称
                roleCode: data.field.roleCode, // 角色代码
                type: data.field.type,
                description: data.field.description, // 角色描述
                permissionIds: checked, // 功能权限
                channelIds: channelIds, // 数据权限(栏目)
                infoIds: infoIds,// 数据权限(信息)
                csrfToken: data.field.csrfToken // CSRF令牌
            };
            $.ajax({
                url: baseURL + 'role/add',
                data: JSON.stringify(formData),
                dataType: 'json',
                contentType: 'application/json',
                type: 'post',
                success: function (result) {
                    $.ajax({
                        url: baseURL + "config",
                        type: 'get',
                        success: function (param) {
                            // 发送消息到父页面
                            window.parent.parent.postMessage({
                                type: 'updateMenu',
                                data: param
                            }, '*');
                        }
                    })
                    if (result.code === 1000) {
                        layer.msg(result.msg, {
                            icon: 1,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("role-table");
                        });
                    } else {
                        layer.msg(result.msg, {
                            icon: 2,
                            time: 2000
                        }, function () {
                            parent.layer.close(parent.layer.getFrameIndex(window
                                .name)); //关闭当前页
                            parent.layui.table.reload("role-table");
                        });
                    }
                    // 5秒后重新启用按钮
                    setTimeout(function () {
                        submitButton.removeClass('layui-btn-disabled').removeAttr('disabled');
                    }, 5000);
                }
            })
            return false;
        });
    })
</script>
</body>
</html>
